package com.weareint.feigncopy;

import org.apache.commons.collections.CollectionUtils;

import java.util.concurrent.TimeUnit;

/**
 *
 *
 * <pre>
 *  缓存映射 接口
 *  提供 mapping 方法 入参 TimeUnit timeUnit, long duration 换成相关信息
 * </pre>
 *
 * @author johnny
 * @date 2021-08-21 11:39 下午
 */
public interface CacheMappingFeignDataService<S, D, T>
        extends MappingFeignDataService<S, D, T>, FeignCache<D, T> {


    /**
     * 缓存mapping
     *
     * @param timeUnit : 缓存单位
     * @param duration : 缓存时长
     */
    default void mapping(TimeUnit timeUnit, long duration) {
        FeignMappingStrategy feignMappingStrategy = getFeignMappingStrategy();
        if (feignMappingStrategy != null) {
            if (CollectionUtils.isEmpty(data())) {
                return;
            }
            if (timeUnit == null || duration == 0) {
                feignMappingStrategy.mapping(data(), fromCache(null));
            }
            feignMappingStrategy.mapping(data(), fromCache(cacheTime(timeUnit, duration)));
        }
    }


}
